# -*- coding: utf-8 -*-

# File Name： launch
# Description :
# Author : lirui
# create_date： 2022/9/13
# Change Activity:
import copy

import ml
from ml.third_party.csv.xls_utility import read_all_sheet_xls_mapping, write_sheets_mapping
from modules.generate_back_view.back_view_cabinet_set import BACK_VIEW_CABINET_SET


def _is_back_view(opposite_end):
    """

    Args:
        opposite_end:

    Returns:

    """
    for bv in BACK_VIEW_CABINET_SET:
        if bv in opposite_end:
            return True
    return False


def _transformer_data(data):
    """

    Args:
        data:

    Returns:

    """
    new_data = copy.deepcopy(data)
    for k in new_data:
        if str(k).startswith('本'):
            old_local_value = new_data[k]
            rep_k = k.replace('本', '对')
            old_opposite_vale = new_data[rep_k]
            new_data[k] = old_opposite_vale
            new_data[rep_k] = old_local_value
    return new_data


def _generate_back_view_mapping(sheets_mapping):
    """

    Args:
        sheets_mapping:

    Returns:

    """
    back_view_sheets_mapping = {}
    for sheet_name in sheets_mapping:
        sheet_data = sheets_mapping[sheet_name]
        for data in sheet_data:
            opposite_end = data['对端地址']
            if opposite_end == '11.2.D02':
                print(1)
            if _is_back_view(opposite_end):
                if opposite_end not in back_view_sheets_mapping:
                    back_view_sheets_mapping[opposite_end] = []
                new_data = _transformer_data(data)
                back_view_sheets_mapping[opposite_end].append(new_data)
    return back_view_sheets_mapping


def _generate_xls(back_view_mapping):
    write_sheets_mapping('背视图.xls', back_view_mapping)


def _main():
    xls_path = 'resource/富滇银行新大楼-标签-设备位置统计信息_20220908_v1.8.xls'
    xls_path = ml.join_relative_path(xls_path)
    ml.logger.info(rf'xls path {xls_path}')
    sheets_mapping = read_all_sheet_xls_mapping(xls_path)
    back_view_mapping = _generate_back_view_mapping(sheets_mapping)
    _generate_xls(back_view_mapping)


if __name__ == '__main__':
    _main()
